﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class TipoPagoRepository: MasterRepository, ITipoPagoRepository
    {
        public List<TIPO_PAGO> GetAllFromTipoPago()
        {
            var query = from tp in context.TIPO_PAGO
                        select tp;
            return query.ToList();
        }

        public List<TIPO_PAGO> GetAllFromTipoPagoByDescripcion(string descripcion)
        {
            var query = from tp in context.TIPO_PAGO
                        select tp;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from tp in context.TIPO_PAGO
                        where tp.Descripcion.ToUpper().Contains(descripcion)
                        select tp;
            }
            return query.ToList();
        }

        public bool ExisteDuplicidad(string descripcion, string codigo, int id)
        {
            var query = from tp in context.TIPO_PAGO
                        where (tp.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 || tp.Codigo.ToUpper().CompareTo(codigo.ToUpper()) == 0) && tp.IdTipoPago  != id
                        select tp;
            return query.Any();
        }

        public void AddTipoPago(TIPO_PAGO tipoPago)
        {
            tipoPago.CONTRATOS = null;
            context.TIPO_PAGO.Add(tipoPago);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                System.IO.File.AppendAllLines(@"c:\errors.txt", outputLines);
                throw;
            }
        }

        public void UpdateTipoPago(TIPO_PAGO tipoPago)
        {
            tipoPago.CONTRATOS = null;
            context.Entry(tipoPago).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            } 
        }

        public void RemoveTipoPago(int id)
        {
            var existe = context.TIPO_PAGO.Find(id);

            if (existe != null)
            {
                context.TIPO_PAGO.Remove(existe);
                context.SaveChanges();
            }
        }


        public TIPO_PAGO GetFromTipoPagoByCodigo(string codigo)
        {
            var query = from rs in context.TIPO_PAGO
                        where rs.Codigo.Equals(codigo)
                        select rs;
            return query.SingleOrDefault();
        }


        public TIPO_PAGO GetFromTipoPagolById(int id)
        {
            var query = from p in context.TIPO_PAGO
                        where p.IdTipoPago.Equals(id)
                        select p;
            return query.SingleOrDefault();
        }
    }
}
